<?php
    use User\Model\MemberServiceModel;
    use User\Model\MemberServiceItemModel;
?>
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                会员信息
            </header>
            <div class="panel-body">
                <form class="form-horizontal" method="post" enctype="multipart/form-data"
                      action="<?php if($type=='add'){echo '/User/Member/memberAdd';}
                      else{echo '/User/Member/memberUpdate';}?>" style="width: 70%;" >
                    <input type="hidden" value="<?php $val = (!empty($member)) ?
                            $member['member_id'] : ''; echo $val;?>" name="memberId">
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">名称:</label>

                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="name" value="<?php $val=(!empty($member)) ?
                            $member['name'] : ''; echo $val;?>">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">电话:</label>

                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="phone" value="<?php $val=(!empty($member)) ?
                            $member['phone'] : ''; echo $val;?>">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">会员类别:</label>

                        <div class="col-sm-10">
                            <select class="form-control" name="class_id" id="class_id">
                                <?php
                                echo "<option>-</option>";
                                foreach($member_class as $key => $val_class){
                                    echo "<option value='" . $val_class['class_id'] . "'";
                                    if(!empty($member) && $member['class_id'] == $val_class['class_id']){
                                        echo " selected ";
                                    }
                                    echo ">" . $val_class['name'] . "</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">地址:</label>

                        <div class="col-sm-10">
                            <select id= 'province' style="width: 200px" name="province">
                            <?php
                            $province_echo = "<option>-</option>";
                            foreach($province as $val_province){
                                $province_echo .= "<option value='" . $val_province['id'] . "'";
                                if(!empty($member) && $member['province'] == $val_province['id']){
                                    $province_echo .= " selected ";
                                }
                                $province_echo .= ">" .$val_province['regionname']. "</option>";
                            }
                                echo $province_echo;
                                ?>
                            </select>
                            <select id= 'city' style="width: 200px" name="city"><option>-</option></select>
                            <select id= 'district' style="width: 200px" name="district"><option>-</option></select>
                        </div>
                    </div>
                    <div class="form-group form-last">
                        <label class="col-sm-2 col-sm-2 control-label need">详细地址:</label>

                        <div class="col-sm-10">
                                <textarea class="form-control" name="address"><?php
                                    if(!empty($member)){
                                        echo $member['address'];
                                    }
                                ?></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-10">
                            <input class="btn btn-primary" type="submit" id="submit" value="提交">
                            <button class="btn btn-default" type="button" onclick="history.back(-1);">取消</button>
                        </div>
                    </div>

                </form>
            </div>
        </section>
    </div>
</div>

<script>
    $(".need").each(function(){
        $(this).prepend("<span style='color:red'>*</span>");
    });
</script>
<script type="text/javascript" src="/Public/js/laydate/laydate.js"></script>
<script type="text/javascript" src="/Public/js/common.upload.hk.js"></script>
<script type="text/javascript">
    var ext = "<?php echo $exist_ext;?>";
    var exist_ext_string = [];
    var exist_ext = [];
    var ext_arr = [];
    if(ext != ''){
        exist_ext_string = ext.split(",");
    }
    if(exist_ext_string.length > 1){
        for(var k=0; k<exist_ext_string.length - 1; k++){
            ext_arr = exist_ext_string[k].split("|");
            exist_ext.push(ext_arr);
        }
    }
    var type = "<?php echo $type;?>";
    $( ".date-picker" ).bind( "click", function(){
        laydate( {
            format: 'YYYY/MM/DD hh:mm:ss',
            min: laydate.now(), //设定最小日期为当前日期
            max: '2099-06-16 23:59:59', //最大日期
            istime: true,
            istoday: false
        } );
        laydate.skin( "molv" );
    });
    var city = "<?php echo !empty($member) ? $member['city'] : -1;?>";
    var district = "<?php echo !empty($member) ? $member['district'] : -1;?>";
    $("#province").bind("change",function(){
        get_city();
    });
    get_city();
    function get_city(){
        $.ajax({
            data:{
                'province':$("#province").val()
            },
            type:'get',
            dataType:'json',
            url:'/User/Member/city',
            success:function(res){
                var html = "<option value=''>-</option>";
                if(res != null) {
                    var len = res.length;
                    for (var i = 0; i < len; i++) {
                        html += "<option value='" + res[i]['id'] + "'";
                        if(res[i]['id'] == city){
                            html += " selected ";
                        }
                        html += ">" + res[i]['regionname'] + "</option>";
                    }
                }
                $("#city").html(html);
            }

        });
    }
    $("#city").bind("change",function(){
        get_district($(this).val());
    });
    get_district(city);
    function get_district(exist_city){
        $.ajax({
            data:{
                'city':exist_city
            },
            type:'get',
            dataType:'json',
            url:'/User/Member/district',
            success:function(res){
                var html = "<option value=''>-</option>";
                if(res != null){
                    var len = res.length;
                    for(var i=0;i<len;i++){
                        html += "<option value='" + res[i]['id'] + "'";
                        if(res[i]['id'] == district){
                            html += " selected ";
                        }
                        html += ">" + res[i]['regionname'] + "</option>";
                    }
                }
                $("#district").html(html);
            }

        });
    }
    $("#class_id").bind("change",function(){
        $(".ext").remove();
        load_ext();
    });
    load_ext();
    function load_ext(){
        $.ajax({
            type:'get',
            dataType:'json',
            data:{
                'class_id':$("#class_id").val()
            },
            url:'/User/Member/ext',
            success:function(res){
                var ext = '';
                var file_count = '';
                if(res != null){
                    var len = res.length;
//                    console.log(len);
                    for(var i=0; i < len; i++){
//                        console.log(i);
                        ext += "<div class='form-group ext'><label class='col-sm-2 col-sm-2 control-label'>";
                        if(res[i]['style'] == 1){
                            ext += "<span style='color:red'>*</span>";
                        }
                        ext += res[i]['name'] + "</label><div class='col-sm-10'>";
                        var j = 0;
                        if(res[i]['format'] == 0){
                            ext += "<input class='form-control' type='text' name='" + res[i]['property_id'] + "'";
                            if(type != 'add' && exist_ext != null && exist_ext.length > 0){
                                for(j=0;j<exist_ext.length;j++){
                                    if($.inArray(res[i]['property_id'], exist_ext[j]) != -1){
                                        ext += " value='" + exist_ext[j][1] + "' ";
                                    }
                                }
                            }
                            ext += "></div>";
                        }else{
                            ext += "<img width='100%' height='200px' src='";
                            if(type != 'add' && exist_ext != null && exist_ext.length > 0){
                                for(j=0;j<exist_ext.length;j++){
                                    if($.inArray(res[i]['property_id'], exist_ext[j]) != -1){
                                        ext += exist_ext[j][1];
                                    }
                                }
                            }
                            ext += "' alt=' ' class='thumbnail preview'";
                            ext += " data-preview='uploadFile" + file_count + "'>";
                            if(type != 'add' && exist_ext != null && exist_ext.length > 0){
                                for(j=0;j++;j<exist_ext.length){
                                    if($.inArray(res[i]['property_id'], exist_ext[j]) != -1){
                                        ext += "<input type='hidden' name='uploadFile" + file_count + "' value='" + exist_ext[j][1] + "' data-set='uploadFile" + file_count + "'>";
                                    }
                                }
                            }
                            ext += "<input type='file' data-action='uploadFile" + file_count + "' name='" + res[i]['property_id'] + "'>";
                            if(file_count == ''){
                                file_count = 1;
                            }else{
                                file_count++;
                            }
                        }
                        ext += "</div>";
                    }
                }
//                console.log(ext);
                $(".form-last").after(ext);
                common.upload.img.init();
            }
        });
    }
</script>

